﻿using System;
using Borg.Properties;

namespace Borg
{
    public sealed class LogFormatter : ILogFormatter
    {
        public LogArgs FormatContractAdded(object source, Contract contract)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_AssemblyDiscovered,
                new object[]
                    {
                        "Contract",
                        contract.Id,
                        contract.Name,
                        contract.OriginUri
                    });
        }

        public LogArgs FormatSlotDefinitionAdded(object source, SlotDefinition slotDefinition)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementFound,
                new object[]
                    {
                        "Slot definition",
                        slotDefinition.Id,
                        slotDefinition.Name
                    });
        }

        public LogArgs FormatSlotDefinitionRemoved(object source, SlotDefinition slotDefinition)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementRemoved,
                new object[]
                    {
                        "Slot definition",
                        slotDefinition.Id,
                        slotDefinition.Name
                    });
        }

        public LogArgs FormatContractRemoved(object source, Contract contract)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_AssemblyRemoved,
                new object[]
                    {
                        "Contract",
                        contract.Id,
                        contract.Name,
                        contract.OriginUri
                    });
        }

        public LogArgs FormatPluginAdded(object source, Plugin plugin)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_AssemblyDiscovered,
                new object[]
                    {
                        "Plugin",
                        plugin.Id,
                        plugin.Name,
                        plugin.OriginUri
                    });
        }

        public LogArgs FormatExtensionTypeAdded(object source, ExtensionType extensionType)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementFound,
                new object[]
                    {
                        "Extension type",
                        extensionType.Id,
                        extensionType.Name
                    });
        }

        public LogArgs FormatPlugTypeAdded(object source, PlugType plugType)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementFound,
                new object[]
                    {
                        "Plug type",
                        plugType.Id,
                        plugType.Name
                    });
        }

        public LogArgs FormatSlotTypeAdded(object source, SlotType slotType)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementFound,
                new object[]
                    {
                        "Slot type",
                        slotType.Id,
                        slotType.Name
                    });
        }

        public LogArgs FormatSlotTypeRemoved(object source, SlotType slotType)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementRemoved,
                new object[]
                    {
                        "Slot type",
                        slotType.Id,
                        slotType.Name
                    });
        }

        public LogArgs FormatPlugTypeRemoved(object source, PlugType plugType)
        {
            return new LogArgs(
                LogType.Info,
                source, Messages.Discovery_ElementRemoved,
                new object[]
                    {
                        "Plug type",
                        plugType.Id,
                        plugType.Name
                    });
        }

        public LogArgs FormatExtensionTypeRemoved(object source, ExtensionType extensionType)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_ElementRemoved,
                new object[]
                    {
                        "Extension type",
                        extensionType.Id,
                        extensionType.Name
                    });
        }

        public LogArgs FormatPluginRemoved(object source, Plugin plugin)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Discovery_AssemblyRemoved,
                new object[]
                    {
                        "Plugin",
                        plugin.Id,
                        plugin.Name,
                        plugin.OriginUri
                    });
        }

        public LogArgs FormatCreated(object source, Extension extension)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionCreated,
                new object[]
                    {
                        extension.Id,
                        extension.Name
                    });
        }

        public LogArgs FormatActivated(object source, Extension extension)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionActivated,
                new object[]
                    {
                        extension.Id,
                        extension.Name
                    });
        }

        public LogArgs FormatDeactivated(object source, Extension extension)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionDeactivated,
                new object[]
                    {
                        extension.Id,
                        extension.Name
                    });
        }

        public LogArgs FormatDestroyed(object source, Extension extension)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionDestroyed,
                new object[]
                    {
                        extension.Id,
                        extension.Name
                    });
        }

        public LogArgs FormatOpened(object source, Slot slot)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_SlotOpened,
                new object[]
                    {
                        slot.Id,
                        slot.Name,
                        slot.Extension.Name
                    });
        }

        public LogArgs FormatClosed(object source, Slot slot)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_SlotClosed,
                new object[]
                    {
                        slot.Id,
                        slot.Name,
                        slot.Extension.Name
                    });
        }

        public LogArgs FormatPlugged(object source, Slot slot, Plug plug)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionPlugged,
                new object[]
                    {
                        plug.Extension.Id,
                        plug.Extension.Name,
                        slot.Extension.Id,
                        slot.Extension.Name,
                        slot.Name
                    });
        }

        public LogArgs FormatUnplugged(object source, Slot slot, Plug plug)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionUnplugged,
                new object[]
                    {
                        plug.Extension.Id,
                        plug.Extension.Name,
                        slot.Extension.Id,
                        slot.Extension.Name,
                        slot.Name
                    });
        }

        public LogArgs FormatTagged(object source, Slot slot, Plug plug, string tag)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionTagged,
                new object[]
                    {
                        plug.Extension.Id,
                        plug.Extension.Name,
                        slot.Extension.Id,
                        slot.Extension.Name,
                        slot.Name,
                        tag
                    });
        }

        public LogArgs FormatUntagged(object source, Slot slot, Plug plug, string tag)
        {
            return new LogArgs(
                LogType.Info,
                source,
                Messages.Composer_ExtensionUntagged,
                new object[]
                    {
                        plug.Extension.Id,
                        plug.Extension.Name,
                        slot.Extension.Id,
                        slot.Extension.Name,
                        slot.Name,
                        tag
                    });
        }

        public LogArgs FormatUnhandledException(object source, Exception unhandledException, bool showStackTrace)
        {
            var stackTrace =
                showStackTrace
                    ? unhandledException.ToString()
                    : unhandledException.Message;

            return new LogArgs(
                LogType.Error,
                source,
                Messages.Runtime_UnhandledException,
                new object[]
                    {
                        AppDomain.CurrentDomain.FriendlyName,
                        stackTrace
                    });
        }
    }
}

